<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Welcome to catalog!</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<link rel="stylesheet" type="text/css" href="js-lib/ext-js/resources/css/ext-all.css">
</head>

<body>
<div id="x-loading-mask" style="width:100%; height:100%; background:#618BAC; position:absolute; z-index:20000; left:0; top:0;">&#160;</div>
  <div id="x-loading-panel" style="position:absolute;left:40%;top:40%;border:1px solid #9c9f9d;padding:2px;background:#d1d8db;width:300px;text-align:center;z-index:20001;">
    <div style="border:1px solid #c1d1d6;color:#666;background:white;padding:10px;margin:0;padding-left: 20px;height:30px;text-align:left;">
      <div id="load-status" style="height:30px;padding-top:5px;"><s:text name="loading.system"/></div>
    </div>
</div>
<script type="text/javascript" src="js-lib/ext-js/adapter/ext/ext-base-debug.js"></script>
<script type="text/javascript" src="js-lib/ext-js/ext-all-debug.js"></script>
<script type="text/javascript" src="js-lib/ext-ux-js/CheckboxField.js"></script>
<script type="text/javascript" src="js-lib/ext-ux-js/ux-all.js" ></script>
<script type="text/javascript" src="js-lib/ext-ux-js/locale/catalog-base-lang-<%=session.getAttribute("WW_TRANS_I18N_LOCALE")%>.js"></script>
<script type="text/javascript" src="js-lib/ext-ux-js/util.js"></script>
<link rel="stylesheet" type="text/css" href="js-lib/ext-ux-js/resources/css/ux-all.css">
<link rel="stylesheet" type="text/css" href="js-lib/ext-ux-js/resources/css/catalog-all.css">
<link rel="stylesheet" type="text/css" href="js-lib/ext-ux-js/resources/css/CheckboxField.css">
<style type="text/css">
.main-header .x-panel-body {
    background:transparent;
}
.icon-expand-all {
    background-image: url(security/images/framework/expand-all.gif) !important;
}
.icon-collapse-all {
    background-image: url(security/images/framework/collapse-all.gif) !important;
}
.x-tree-node div.modules-node{
    background:#eee;
    margin-top:1px;
    border-top:1px solid #ddd;
    border-bottom:1px solid #ccc;
    padding-top:2px;
    padding-bottom:3px;
}
.home {
    background-image: url(security/images/framework/home.gif) !important;
}
.user {
    background-image: url(security/images/framework/user.gif) !important;
}
.settings {
    background-image: url(security/images/framework/menu.gif) !important;
}
.exit {
    background-image: url(security/images/framework/exit.gif) !important;
}
.my-icon {
    background-image: url(security/images/framework/508.png) !important;
}
</style>
<script type="text/javascript">
Ext.BLANK_IMAGE_URL = "js-lib/ext-js/resources/images/default/s.gif";

Ext.ns('catalog.security.framework');
catalog.security.framework.HeaderPanel = Ext.extend(Ext.Panel, {

    homeText:'Acasa',
    userText:'Utilizator',
    addStudentText:'Adauga elev',
    settingsText:'Setari',
    mediiGeneraleText:'Medii generale elevi',
    exitText:'Iesire',
    
    initComponent:function() {
      Ext.apply(this, {
            border :false,
            layout :'anchor',
            region :'north',
            cls: 'main-header',
            height :60,
            items:[{
                xtype :'box',
                border :false,
                el: 'header',
                height :30
            },
            new Ext.Toolbar( {
                border:true,
                items : [{
                    text:'Acasa',
                    tooltip: 'Home',
                    iconCls :'home' ,

                    handler : function() {
                                               window.location.reload() ;
                                         }
                },'->',            { text:"Adauga elev",
                                                                                                          id:'adaugaElev',
                                                                                                            hidden : true,
                                                                                                          tooltip: 'Adaugarea unui nou elev',
                                                                                                          iconCls :'settings',
                                                                                                          handler : function() {
                                                                                                              mainPanel.loadModule('catalog/adaugaElev.js', 'Adaugare elev');
                                                                                                          }
                                                                                                                                              }    ,
                                 { text:"Adauga profesor",
                                                                      tooltip: 'Adaugarea unui nou profesor',
                                                                        id:'adaugaProfesor' ,
                                                                          hidden : true,
                                                                      iconCls :'settings',
                                                                      handler : function() {
                                                                          mainPanel.loadModule('catalog/adaugaProfesor.js', 'Adaugare profesor');
                                                                      }
                                                                      }    ,

                                 { text:"Adauga materie",
                                 tooltip: 'Asocierea unei materii cu un profesor',
                                 iconCls :'settings',
                                   hidden : true,
                                 id:'adaugaMaterie',
                                 handler : function() {
                                 mainPanel.loadModule('catalog/adaugaMaterie.js', 'Adaugare materie');
                                 }
                                 } ,

               {
                    text:'Utilizator',
                    tooltip: 'Setari utilizator',
                    iconCls :'user',
                    handler : function() {
                        mainPanel.loadModule('security/userSetting.js', 'Setari utilizator');
                    }
                },{
                    text:this.exitText,
                    tooltip: 'Exit',
                    iconCls :'exit',
                    handler : function() {
                        window.location = 'j_spring_security_logout';
                    }
                }]
            })
            ]
        }); // eo apply
        catalog.security.framework.HeaderPanel.superclass.initComponent.apply(this, arguments);
    } // eo function initComponent
}); // eo Tutorial.LocalizationWin

msgPanel = function(){

 msgPanel.superclass.constructor.call(this, {
     id:'msg-panel',
     region:'south',
     //layout:'fit',
     height:140,
     border:false,
     title:this.messageText,
     split:true,
     bodyStyle:'padding:4px',
     collapsible: false,
     renderHidden:true,
     autoScroll:true
 });
};
catalog.security.framework.msgPanel = Ext.extend(msgPanel, Ext.Panel, {
    messageText:'Message'
});

MenuTreePanel = function() {


    MenuTreePanel.superclass.constructor.call(this, {
        id:'module-tree',
        region:'center',
        //layout:'fit',
        border:false,
        floatable: false,
        title:this.navPanelText,
        split:true,
        minSize: 175,
        maxSize: 400,
        collapsible: true,
        minHeight:240,
        rootVisible:false,
        lines:false,
        autoScroll:true,
        loader: new Ext.tree.TreeLoader({
            url:'loadMenu.action'
        }),
        root: new Ext.tree.TreeNode('Module Viewer'),
        collapseFirst:false,

        tbar: [{
            iconCls:'icon-expand-all',
            text:'Extinde tot',
            handler: function(){this.root.expand(true); },
            scope: this
       },{
            iconCls:'icon-collapse-all',
            text:'Restrange tot',
            handler: function(){this.root.collapse(true); },
            scope: this
        }]
    });

    this.root.appendChild(
           new Ext.tree.AsyncTreeNode({
               id:'allModulesRoot',
            text:'Toate modulele',
            cls:'modules-node',
            leaf:false,
            expanded:true
        })
    );
   this.getLoader().on({
        'load' : function(sm, node,r){
            var res = Ext.decode(r.responseText);
            if (res.message){
                showMsg('failure',res.message);
            }
        },
        scope:this    });

};
catalog.security.framework.MenuTreePanel=Ext.extend(MenuTreePanel, Ext.tree.TreePanel, {
    navPanelText:'Navigator',
    expandText:'Extinde tot',
    collapseText:'Restrange tot',
    allModulesText:'Toate modulele'
});


NavPanel = function() {

   NavPanel.superclass.constructor.call(this, {
        id:'nav-panel',
        region:'west',
        layout:'border',
        margins:'0 0 5 5',
        cmargins:'0 5 5 5',
        split:true,
        width: 225,
        minSize: 175,
        maxSize: 400,
        minHeight:600,
        collapsible: true,
        collapseMode:'mini',
        items:[new MenuTreePanel(),new msgPanel()]
    });
};
catalog.security.framework.NavPanel = Ext.extend(NavPanel, Ext.Panel, {
});




MainPanel = function() {
        MainPanel.superclass.constructor.call(this, {
        id:'main-tabs',
        title:"cucu",
        region:'center',
        margins:'0 0 5 0',
        enableTabScroll: true,
        minTabWidth: 75,
        activeTab:0,
        plain:false,
        plugins: new Ext.ux.TabCloseMenu(),
        autoLoad : {url:'security/homePage.js',scripts:true}
    });
};

catalog.security.framework.MainPanel=Ext.extend(MainPanel, Ext.TabPanel, {
    centerPanelText:'Mumu',
    loadModule : function(href,tabTitle){
        var tab;

            var autoLoad = catalog.util.autoLoad({url: href,scripts:true});
            tab = new Ext.Panel({
                id: tabTitle,
                title: tabTitle,
                closable:true,
                autoLoad: autoLoad,
                layout:"fit"
            });
            this.add(tab);

        this.setActiveTab(tab);
    }
});

function showMsg(msgType,msg) {
console.log(msg) ;
    var detailEl = Ext.DomHelper.insertFirst(Ext.getCmp('msg-panel').body, {id:'msg-div',cls : msgType=='failure'?'errorMsg':'infoMsg'}, true);
    var failureMsg = msg;
    if (msgType=='failure'){
       if (msg.indexOf('session expired')!=-1){
            Ext.Msg.show({
                title:mbLocale.infoMsg,
                msg: mbLocale.sessionExpiredMsg,
                buttons: Ext.Msg.YESNO,
                scope: this,
                fn: function(buttonId, text, opt) {
                    if (buttonId == 'yes') {
                    window.location = 'j_spring_security_logout';
                    }
                },
                icon: Ext.MessageBox.QUESTION
            });
            failureMsg = mbLocale.sessionExpiredMsg;
        }else if(msg.indexOf('(403)')!=-1){
            Ext.Msg.alert('failure',mbLocale.accessDeniedMsg);
            failureMsg = mbLocale.accessDeniedMsg;
        }else{
            Ext.Msg.alert('failure',msg);
        }
    }
    var dt = new Date();
    dt = '<em>&nbsp;' + dt.format('g:i a') + '</em>';
    detailEl.hide().update(failureMsg+dt).slideIn('t');
}

Ext.onReady(function(){
var headerPanel = new catalog.security.framework.HeaderPanel();
var navPanel = new catalog.security.framework.NavPanel();
mainPanel = new catalog.security.framework.MainPanel();
Ext.getCmp('main-tabs').on('load', 'loadRooms.action')   ;
Ext.getCmp('module-tree').on('click', function(node, e){
  if(node.isLeaf()){
     mainPanel.loadModule(node.attributes.url, node.attributes.text);
  }
});
 var viewport = new Ext.Viewport({
  layout:'border',
  items:[
      headerPanel,
      navPanel,
      mainPanel
  ]
});
Ext.get('x-loading-mask').hide();
Ext.get('x-loading-panel').hide();

catalog.util.ajaxRequest({
           url: 'loadHomePage.action',
           success:function(r,o){
               var res = Ext.decode(r.responseText);

                      Ext.state.Manager.setProvider(new Ext.state.CookieProvider());
                         if(res.admin=='admin'){
                          Ext.getCmp('adaugaElev').setVisible(true);
                          Ext.getCmp('adaugaProfesor').setVisible(true);
                          Ext.getCmp('adaugaMaterie').setVisible(true);
                         }



           },
           failure:Ext.emptyFn
       },this);

   
});
</script>
<script type="text/javascript" src="security/locale/catalog-security-lang-<%=session.getAttribute("WW_TRANS_I18N_LOCALE")%>.js"></script>
<script type="text/javascript" src="crm/locale/catalog-crm-lang-<%=session.getAttribute("WW_TRANS_I18N_LOCALE")%>.js"></script>
<s:hidden id="pageSize" name="pageSize"></s:hidden>
<script type="text/javascript" src="js-lib/ext-ux-js/ComplexGrid.js"></script>
<div id="header"></div>
<s:if test="%{skin=='Gray'}" >
<script>
Ext.util.CSS.swapStyleSheet('theme', 'js-lib/ext-ux-js/resources/css/xtheme-gray-extend.css');
</script>
</s:if>
</body>
</html>